//ENEMY
var Enemy = function () {
	this.bounds = {
		x : 0,
		y : 0,
		w : 0,
		h : 0,
		xs: 0,
		ys: 0,
		color : 'rgba('+COLORS['r'][0]+', '
					   +COLORS['g'][0]+', '
					   +COLORS['b'][0]+', 1)'
	}
	this.checkArea = 50; // Observe dans une zone de 50 px
	
	this.type = 0;

	this.particules = [];

	// Init Rectangle	
	this.init = function(axeX,axeY,radius,type) {
		//Défini la position en abscisse
		this.bounds.x = axeX;
		//Défini la position en ordonnée
		this.bounds.y = axeY;
		//Défini la taille
		this.bounds.w = this.bounds.h = radius;

		//Défini les vitess max
		this.bounds.xsMax = 1;
		this.bounds.ysMax = 1;

		//Défini la vitesse horizontale
		this.bounds.xs = Math.random() * 1 - 1 | 1;
		//Défini la vitesse verticale
		this.bounds.ys = Math.random() * 1 - 1 | 1;

		this.bounds.speedStepX = Math.round(GAME_SPEED*this.bounds.xs/100);
		this.bounds.speedStepY = Math.round(GAME_SPEED*this.bounds.ys/100);

		this.changeColor(type);
	}

	this.changeColor = function(type) {
		this.type = type;
		//Défini la couleur
		this.bounds.color = 'rgba(' +COLORS['r'][type]+', '
									+COLORS['g'][type]+', '
									+COLORS['b'][type]+', '
									+COLORS['a'][type]+')';

		this.type = type;
	}

	// Mise à jour de l'élément
	this.update = function(){

		if(this.type != 0){
			if(this.type == game.p1.followers)
				this.followPlayer(game.xFollower,game.yFollower);
			if(this.type == game.p1.avoiders)
				this.avoidPlayer();
		}

		if (game.bonus != null) {
			if (game.bonus.type == 'nocolor' && game.bonusState) this.avoidPlayer();
		}

		this.move();
		animParticles(this.particules, 'enemy');
	}
	
	// Affichage
	this.render = function(){
		ctx.shadowBlur = 0;
		var color = this.bounds.color;
		if (game.bonus != null) {
			if (game.bonus.type == 'nocolor' && game.bonusState) {
				color = 'rgba(' +COLORS['r'][0]+', ' 
										+COLORS['g'][0]+', '
										+COLORS['b'][0]+', '
										+COLORS['a'][0]+')';
			}
		}
  
		drawRect(ctx,color,this.bounds.x,this.bounds.y,this.bounds.w,this.bounds.h);

		renderParticules(this.particules);
	}

	// Gestion de la collision
	// Return False s'il y a collision
	this.collide = function(xPos,yPos) {
		var length = (xPos - this.bounds.x)*(xPos - this.bounds.x) + (yPos - this.bounds.y)*(yPos - this.bounds.y);
		if (length < game.p1.bounds.radius*game.p1.bounds.radius) return false;
		else return true;
	}

	// GESTION DE LA COLLISION
	this.checkBoundary = function(axeX,axeY) {
		if (axeX < 0 || axeY < 0 || axeX+this.bounds.w > MAX_WIDTH || axeY+this.bounds.h > MAX_HEIGHT) return false;
		else return true;	
	}

	// GESTION DE LA COLLISION DANS LE MENU
	this.checkBoundaryInMenu = function(axeX,axeY) {
		if (axeX < (MAX_WIDTH/2-74) || axeY < (MAX_HEIGHT/2-76) || axeX+this.bounds.w > (MAX_WIDTH/2+74) || axeY+this.bounds.h > (MAX_HEIGHT/2+70)) return false;
		else return true;
	}

	//Suivi du joueur
	this.followPlayer = function(x,y){
		if (x != null && y != null) {

	        if (this.bounds.x > x) {
	            this.bounds.xs += this.bounds.speedStepX * game.offSetFollowers * Math.random();
	        }
	        else if (this.bounds.x < x) {
	            this.bounds.xs -= this.bounds.speedStepX * game.offSetFollowers * Math.random();
	        }

	        if (this.bounds.y > y) {
	            this.bounds.ys += this.bounds.speedStepY * game.offSetFollowers * Math.random();
	        }
	        else if (this.bounds.y < y) {
	            this.bounds.ys -= this.bounds.speedStepY * game.offSetFollowers * Math.random();
	        }
	    }
	}

	this.avoidPlayer = function() {
	    if (game.p1.bounds.x != null && game.p1.bounds.y != null) {

	        if (this.bounds.x > game.p1.bounds.x) {
	            this.bounds.xs -= this.bounds.speedStepX * Math.random();
	        }
	        else if (this.bounds.x < game.p1.bounds.x) {
	            this.bounds.xs += this.bounds.speedStepX * Math.random();
	        }

	        if (this.bounds.y > game.p1.bounds.y) {
	            this.bounds.ys -= this.bounds.speedStepY * Math.random();
	        }
	        else if (this.bounds.y < game.p1.bounds.y) {
	            this.bounds.ys += this.bounds.speedStepY * Math.random();
	        }
	    }
	}

	// Mouvement du props
	this.move = function() {
		if(this.bounds.xs > this.bounds.xsMax)
			this.bounds.xs = this.bounds.xsMax;
		if(this.bounds.ys > this.bounds.ysMax)
			this.bounds.ys = this.bounds.ysMax;

		var newX = this.bounds.x - GAME_SPEED*this.bounds.xs;
		var newY = this.bounds.y - GAME_SPEED*this.bounds.ys;
	
		if (game.STATE == 'GAME') {
			if (this.checkBoundary(newX,newY)){
				this.bounds.x -= GAME_SPEED*this.bounds.xs;
				this.bounds.y -= GAME_SPEED*this.bounds.ys;
			}else{
				this.bounds.xs = -this.bounds.xs;
				this.bounds.ys = -this.bounds.ys;
			}
		}
		else if (game.STATE == 'MENU') {
			if (this.checkBoundaryInMenu(newX,newY)){
				this.bounds.x -= GAME_SPEED*this.bounds.xs;
				this.bounds.y -= GAME_SPEED*this.bounds.ys;
			}else{
				this.bounds.xs = -this.bounds.xs;
				this.bounds.ys = -this.bounds.ys;
			}			
		}
	}

	//Explose la particule
	this.explode = function(n){
		this.particules = [];
		var N = ((2 * Math.PI) / n);
		var speed = 5;
		while(n --){
			//Variable selon le n
			var angle = N * n;
			
			this.particules.push(
				{
					x : this.bounds.x + Math.sin(angle) * speed,
					y : this.bounds.y + Math.cos(angle) * speed,
					xs: Math.sin(angle) * speed * Math.random(),
					ys: Math.cos(angle) * speed ,
					l : Math.random() * 70 | 1,
					s : 2,
					c : this.bounds.color
				}
			);
		}
	}

	// Kill d'un ennemie
	this.kill = function() {
		game.p1.toKill--;
		game.SCORE += 200;
		this.explode(15);
	}

	// Repop d'un ennemi
	this.repop = function(nbEnemy) {
		if (this.bounds.x <= MAX_WIDTH/2) this.bounds.x = MAX_WIDTH - this.bounds.w*2;
		else this.bounds.x = 0 + this.bounds.w*2;
		this.bounds.y = rand(100,MAX_HEIGHT-100);
		if (nbEnemy > 1 && game.nbEnemies <= 250) {
			game.generateEnemy((nbEnemy-1),this.type);
		}
		else if (game.nbEnemies >= 250) {
			this.bounds.xsMax++;
			this.bounds.ysMax++;
			if (game.SCORE > 100000) {
				this.bounds.xsMax+=2;
				this.bounds.ysMax+=2;
			}
			if (game.SCORE > 150000) {
				this.bounds.xsMax+=3;
				this.bounds.ysMax+=3;
			}
			if (game.SCORE > 200000) {
				this.bounds.xsMax+=4;
				this.bounds.ysMax+=4;
			}
		}
	}

};